<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: IT宇宙人
 * Date: 2015-09-09
 */
namespace app\common\model;

use think\Db;
use think\Model;

class Users extends Model
{
    //自定义初始化
    protected static function init()
    {
        //TODO:自定义的初始化
    }

    /**
     * 用户下线分销金额
     * @param $value
     * @param $data
     * @return float|int
     */
    public function getRebateMoneyAttr($value, $data){
        $sum_money = DB::name('rebate_log')->where(['status' => 3,'user_id'=>$data['user_id']])->sum('money');
        $rebate_money = empty($sum_money) ? (float)0 : $sum_money;
        return  $rebate_money;
    }

    /**
     * 用户一级下线数
     * @param $value
     * @param $data
     * @return mixed
     */
    public function getFisrtLeaderNumAttr($value, $data){
        $fisrt_leader = Users::where(['first_leader'=>$data['user_id']])->count();
        return  $fisrt_leader;
    }

    /**
     * 用户二级下线数
     * @param $value
     * @param $data
     * @return mixed
     */
    public function getSecondLeaderNumAttr($value, $data){
        $second_leader = Users::where(['second_leader'=>$data['user_id']])->count();
        return  $second_leader;
    }

    /**
     * 用户二级下线数
     * @param $value
     * @param $data
     * @return mixed
     */
    public function getThirdLeaderNumAttr($value, $data){
        $third_leader = Users::where(['third_leader'=>$data['user_id']])->count();
        return  $third_leader;
    }

    public function addData($data){
        header('Content-Type: text/html; charset=utf-8');
        $insert['mobile']       = isset($data['mobile'])?$data['mobile']:"";
        $insert['nickname']     = $this->filterEmoji($data['nickname']);
        $insert['sex']          = $data['sex'];
        $insert['head_pic']     = $data['head_pic'];
        $insert['tk_id']        = $data['tk_id'];
        $insert['wxapp_openid'] = $data['wxapp_openid'];
        $insert['session_key']  = $data['session_key']?:"";
        $insert['reg_time']     = time();
        $insert['last_login']   = time();
        $uid = $this->insertGetId($insert);
        if (!$uid) {
            return false;
        }
        return $uid;
    }

    //微信昵称过滤emoji表情
    private function filterEmoji($str)
    {
        $str = preg_replace_callback( '/./u',
            function (array $match) {
                return strlen($match[0]) >= 4 ? '' : $match[0];
            },
            $str);

        return $str;
    }

    // 小程序注册登录
    public function wxappRegister($post,$wxappOpenid){
        $data['wxapp_openid'] = $wxappOpenid;
        $data['head_pic']     = $post['avatarUrl'];
        $data['sex']          = $post['gender'];
        $data['nickname']     = $post['nickName'];
        $data['session_key']  = $post['session_key']?:"";
        $data['tk_id']        = !empty(cookie('tk_id')) ? cookie('tk_id') : 1;
        $userId = $this->addData($data);
        if(!$userId){
            return false;
        }
        return $this->where(['user_id'=>$userId])->find();
    }

    /**
     * @param $user_id
     * @param int $page
     * @param int $num
     * @param string $order
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getSonList($user_id, $page = 1, $num = 10, $order = 'level desc, user_id desc')
    {
        return $this->where(['tk_id' => $user_id])->page($page, $num)->order($order)->select();
    }

}
